﻿using ChuanYe.Core.Utils;
using ChuanYe.CountyFireManagement.EF.MySQL;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;

namespace ChuanYe.CountyFireManagement.BLL
{
    public class FireInfoBLL
    {
        #region 构造单例
        private FireInfoBLL() { }
        private static FireInfoBLL _instance;
        public static FireInfoBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new FireInfoBLL());
            }
        }
        #endregion


        public ResultDto<List<FireMQTTResponseModel>> Add(Fireinfo fireInfo, List<Firesite> fireSites, List<Smsinfo> smsInfos)
        {
            ResultDto<List<FireMQTTResponseModel>> result = new ResultDto<List<FireMQTTResponseModel>>();
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                using (TransactionScope trans = new TransactionScope())
                {
                    //火灾
                    ef.Fireinfo.Add(fireInfo);
                    ef.Entry<Fireinfo>(fireInfo).State = EntityState.Added;
                    ef.SaveChanges();

                    //处置记录
                    Record record = new Record();
                    record.FireId = fireInfo.FireId;
                    record.HandleTime = fireInfo.HappenDatetime;
                    record.Type = (int)RecordType.火灾录入;
                    record.UserId = fireInfo.UserId;
                    string happenDatetime = fireInfo.HappenDatetime.Value.ToString("yyyy年MM月dd日HH时mm分");
                    string mrContent = string.Format(FireInfoTool.GetFireInfoRecordFormat(record.Type.Value), happenDatetime, fireInfo.Alarm, fireInfo.Address, fireInfo.AlarmContent);
                    record.Content = mrContent;
                    record.AddTime = fireInfo.TransDatetime;
                    record.Ip = fireInfo.Ip;
                    record.Applicant = fireInfo.Alarm;
                    record.CustomData = fireInfo.FireId.ToString();
                    ef.Record.Add(record);
                    ef.Entry<Record>(record).State = EntityState.Added;

                    //消息
                    Message message = new Message();
                    message.FireId = fireInfo.FireId;
                    message.Type = (int)MessageType.接收火灾;
                    message.Content = string.Format(FireInfoTool.GetFireInfoMessageFormat(message.Type), fireInfo.FireName);
                    message.SendType = (int)MessageSendType.下级向上级发送;
                    message.IsRead = 0;
                    message.HandleTime = fireInfo.HappenDatetime;
                    message.SendUserId = fireInfo.UserId.Value;
                    message.Ip = fireInfo.Ip;
                    ef.Message.Add(message);
                    ef.Entry<Message>(message).State = EntityState.Added;
                    ef.SaveChanges();

                    string firstTime = fireInfo.TransDatetime.Value.AddMinutes(10).ToString("yyyy年MM月dd日HH时mm分");
                    Message sbMessage = new Message();
                    sbMessage.FireId = fireInfo.FireId;
                    sbMessage.Type = (int)MessageType.首报倒计时提示;
                    sbMessage.Content = string.Format(FireInfoTool.GetFireInfoMessageFormat(sbMessage.Type), fireInfo.FireName, firstTime);
                    sbMessage.SendType = (int)MessageSendType.上级向下级发送;
                    sbMessage.IsRead = 0;
                    sbMessage.HandleTime = fireInfo.HappenDatetime;
                    sbMessage.SendUserId = fireInfo.UserId.Value;
                    sbMessage.Ip = fireInfo.Ip;
                    ef.Message.Add(sbMessage);
                    ef.Entry<Message>(sbMessage).State = EntityState.Added;
                    ef.SaveChanges();

                    //过期等待
                    Fireexpire fireexpire = new Fireexpire();
                    fireexpire.StartTime = fireInfo.TransDatetime.Value;
                    fireexpire.Value = 10;
                    fireexpire.FireId = fireInfo.FireId;
                    fireexpire.RecordType = (int)RecordType.首报未按时操作;
                    fireexpire.MessageType = (int)MessageType.首报未作提示;
                    string msContent = string.Format(FireInfoTool.GetFireInfoMessageFormat(fireexpire.MessageType), fireInfo.FireName, firstTime);
                    fireexpire.MessageContent = msContent;
                    ef.Fireexpire.Add(fireexpire);
                    ef.Entry<Fireexpire>(fireexpire).State = EntityState.Added;
                    ef.SaveChanges();
                    //定位火点
                    foreach (var item in fireSites)
                    {
                        item.RecordType = record.Type.Value;
                        item.FireId = fireInfo.FireId;
                        ef.Firesite.Add(item);
                        ef.Entry<Firesite>(item).State = EntityState.Added;
                        ef.SaveChanges();
                    }

                    //短信
                    foreach (var item in smsInfos)
                    {
                        ef.Smsinfo.Add(item);
                        ef.Entry<Smsinfo>(item).State = EntityState.Added;
                        ef.SaveChanges();
                    }

                    //用户大区推算
                    var exitUserAreaProbabilityList = ef.Userareaprobability.Where(p => p.UserId == fireInfo.UserId.Value).ToList();
                    foreach (var item in exitUserAreaProbabilityList)
                    {
                        ef.Userareaprobability.Add(item);
                        ef.Entry<Userareaprobability>(item).State = EntityState.Deleted;
                        ef.SaveChanges();
                    }
                    var existUser = ef.Userinfo.Where(p => p.UserId == fireInfo.UserId.Value).FirstOrDefault();
                    Userareaprobability userAreaProbability = new Userareaprobability();
                    userAreaProbability.RoleId = existUser.RoleId;
                    userAreaProbability.AddressCounty = fireInfo.AddressCounty;
                    userAreaProbability.AddressTown = fireInfo.AddressTown;
                    userAreaProbability.AddressVillage = fireInfo.AddressVillage;
                    userAreaProbability.UserId = fireInfo.UserId.Value;
                    userAreaProbability.AddTime = DateTime.Now;
                    ef.Userareaprobability.Add(userAreaProbability);
                    ef.Entry<Userareaprobability>(userAreaProbability).State = EntityState.Added;

                    ef.SaveChanges();
                    trans.Complete();

                    #region 装载消息
                    List<Message> messages = new List<Message>() { message, sbMessage };
                    result.FirstParam = MessageBLL.Instance.TransMessageToFireMQTTResponseModel(messages,record.Type.Value);
                    #endregion

                }
                return result;
            }
        }

        public ResultDto<dynamic, dynamic> GetFireInfoByPage(int isSate,string addressCounty, List<string> addressTowns, RequestPageBase page)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            var sql = AppSetting.GetConfig("Sql:FireInfoBLL.GetFireInfoByPage");
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                string paramsSql = string.Empty;
                if (!string.IsNullOrEmpty(addressCounty))
                {
                    paramsSql += " and AddressCounty = '{0}'";
                    paramsSql = string.Format(paramsSql, addressCounty);
                }
                if (addressTowns != null && addressTowns.Count > 0)
                {
                    string addressTownsParms = string.Join(",", addressTowns);
                    paramsSql += " and AddressTown  in ({0})";
                    paramsSql = string.Format(paramsSql, addressTownsParms);
                }

                paramsSql += " and IsState='{0}'";
                paramsSql = string.Format(paramsSql, isSate);

                var timeSql = page.TimeSql(" a.AlarmTime ");
                var keywordSql = page.KeywordSql(" a.FireName ");
                paramsSql += timeSql;
                paramsSql += keywordSql;
                sql = string.Format(sql, paramsSql);
                var countSql = sql.CountSql();
                result.SecondParam = ef.Database.SqlQuery(countSql).Rows[0][0];

                sql += page.OrderByPattern == 0 ? "a.AlarmTime".OrderByDescending() : "a.AlarmTime".OrderBy();
                sql += page.Limit();
                var list = ef.Database.SqlQuery<FireInfoModel>(sql);
                var fireIds = list.Select(s => s.FireId).ToList();

                var lastRecords=BLL.RecordBLL.Instance.GetLast(fireIds,null);

                foreach (var item in list)
                {
                    var lastRecord = lastRecords.Where(p => p.FireId == item.FireId).FirstOrDefault();
                    if (lastRecord != null)
                    {
                        item.RecordType = lastRecord.Type;
                    }
                }
                result.FirstParam = list;

                
            }
            return result;
        }

        public ResultDto<dynamic> GetFireInfoDetail(int fireId, List<int> ignoreRecordTypes)
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var linq = from a in ef.Record select a;
                linq = linq.Where(p => p.FireId == fireId);
                if (ignoreRecordTypes != null && ignoreRecordTypes.Count > 0)
                {
                    linq = linq.Where(p => !ignoreRecordTypes.Contains(p.Type.Value));
                }
                List<Record> records = linq.OrderByDescending(o => o.RecordId).ToList();
                var rSimples = records.OrderBy(o => o.RecordId).Select(s => new { Type=s.Type.Value ,s.CustomData}).ToList();

                List<dynamic> list = new List<dynamic>();

                foreach (var item in rSimples)
                {
                    int id =0;
                    List<int> ids = new List<int>();
                    int.TryParse(item.CustomData, out id);
                    if (id == 0)
                    {
                        ids = item.CustomData.Split(',').ToArray().Select<string, int>(s => Convert.ToInt32(s)).ToList();
                    }
                    switch (item.Type)
                    {
                        #region 火警录入
                        case (int)RecordType.火灾录入:
                            var fireInfo = (from a in ef.Fireinfo.Where(p => p.FireId == fireId).ToList()
                                            join b in ef.Userinfo on a.UserId equals b.UserId
                                            into c
                                            let d = c.FirstOrDefault()
                                            select new
                                            {
                                                a.FireId,
                                                a.AlarmTime,
                                                a.Alarm,
                                                a.AlarmContent,
                                                a.FireName,
                                                a.ChangeFireId,
                                                a.HappenDatetime,
                                                a.Ip,
                                                a.IsSupplementary,
                                                a.ReporterUnit,
                                                a.ReporterWorknum,
                                                a.TransDatetime,
                                                a.ReporterName,
                                                a.ReporterPhone,
                                                a.IsState,
                                                a.IsChange,
                                                a.Smscontent,
                                                a.AddressCounty,
                                                a.AddressTown,
                                                a.AddressVillage,
                                                a.Address,
                                                a.UserId,
                                                d.RealName
                                            }).FirstOrDefault();
                            list.Add(new { Key = item.Type, Module = "FireInfo", Value = fireInfo, Remark = "火警录入" });
                            break;
                        #endregion
                        #region 火灾首报
                        case (int)RecordType.火灾首报:
                            var fireFirstReport =
                                       (from a in ef.Firefirstreport.Where(p => p.FireId == fireId && p.FirstReportId == id).ToList()
                                        join b in ef.Userinfo on a.UserId equals b.UserId
                                        into c
                                        let d = c.FirstOrDefault()
                                        select new
                                        {
                                            a.FirstReportId,
                                            a.FireId,
                                            a.AddTime,
                                            a.TransDatetime,
                                            a.FirstReportContent,
                                            a.Ip,
                                            a.UserId,
                                            d.RealName,
                                        }).FirstOrDefault();
                            list.Add(new { Key = item.Type, Module = "FireFirstReport", Value = fireFirstReport, Remark = "火灾首报" });
                            break;
                        #endregion
                        #region 火灾续报
                        case (int)RecordType.火灾续报:
                            var fireContinueReport =
                                       (from a in ef.Firecontinuereport.Where(p => p.FireId == fireId && p.ContinueReportId == id).ToList()
                                        join b in ef.Userinfo on a.UserId equals b.UserId
                                        into c
                                        let d = c.FirstOrDefault()
                                        select new
                                        {
                                            a.ContinueReportId,
                                            a.FireId,
                                            a.AddTime,
                                            a.TransDatetime,
                                            a.ContinueReportContent,
                                            a.Ip,
                                            a.UserId,
                                            d.RealName,
                                        }).FirstOrDefault();
                            list.Add(new { Key = item.Type, Module = "FireContinueReport", Value = fireContinueReport, Remark = "火灾续报" });
                            break;
                        #endregion
                        #region 外勤调动|外勤核实
                        case (int)RecordType.外勤调动:
                        case (int)RecordType.外勤核实:
                            var fireField =
                                       (from a in ef.Firefield.Where(p => p.FireId == fireId && p.FieldId == id).ToList()
                                        join b in ef.Userinfo on a.UserId equals b.UserId
                                        into c
                                        let d = c.FirstOrDefault()
                                        join e in ef.Userinfo on a.SceneUserId equals e.UserId
                                        into f
                                        let g = f.FirstOrDefault()
                                        join h in ef.Firefieldattach on a.FireId equals h.FieldId
                                        into i 
                                        let j=i.FirstOrDefault()
                                        select new FirefieldModel
                                        {
                                            FieldId = a.FieldId,
                                            FireId = a.FireId,
                                            TransDatetime = a.TransDatetime,
                                            FieldContent = a.FieldContent,
                                            RecordType = a.RecordType,
                                            SendMode = a.SendMode,
                                            SendStatus = a.SendStatus,
                                            SceneTime = a.SceneTime,
                                            SceneUserId = a.SceneUserId,
                                            Ip = a.Ip,
                                            UserId = a.UserId,
                                            RealName = d.RealName,
                                            SceneUserRealName = g == null ? string.Empty : g.RealName,
                                            EstimateLatitude=a.EstimateLatitude,
                                            EstimateLongitude = a.EstimateLongitude,
                                            EstimateAltitude=a.EstimateAltitude,
                                            ReviseLatitude = a.ReviseLatitude,
                                            ReviseLongitude = a.ReviseLongitude,
                                            ReviseAltitude=a.ReviseAltitude
                                        }).FirstOrDefault();
                            if (fireField != null)
                            {
                                var attachs = ef.Firefieldattach.Where(p => p.FieldId == fireField.FieldId).ToList()
                                    .Select(s => new FirefieldattachModel()
                                    {
                                        FieldId = s.FieldId,
                                        Direction = s.Direction,
                                        SceneFile = s.SceneFile,
                                        FieldAttachId = s.FieldAttachId,
                                        ShotLatitude = s.ShotLatitude,
                                        ShotLongitude = s.ShotLongitude,
                                        ShotAltitude=s.ShotAltitude,
                                    }).ToList();
                                fireField.Attachs = attachs;
                            }
                            list.Add(new { Key = item.Type, Module = "FireField", Value = fireField, Remark = "外勤调动|外勤核实" });
                            break;
                        #endregion
                        #region 误报申请|误报审批
                        case (int)RecordType.误报申请:
                        case (int)RecordType.误报审批:
                            var fireMisreport =
                                       (from a in ef.Firemisreport.Where(p => p.FireId == fireId && p.MisreportId == id).ToList()
                                        join b in ef.Userinfo on a.UserId equals b.UserId
                                        into c
                                        let d = c.FirstOrDefault()
                                        join e in ef.Userinfo on a.ReplyUserId equals e.UserId
                                        into f
                                        let g = f.FirstOrDefault()
                                        select new
                                        {
                                            a.MisreportId,
                                            a.FireId,
                                            a.MisreportContent,
                                            a.ReplyContent,
                                            a.ReplyResult,
                                            a.ReplyTime,
                                            a.Ip,
                                            a.UserId,
                                            d.RealName,
                                            a.TransDatetime,
                                            ReplyUserRealName = g == null ? string.Empty : g.RealName,
                                        }).FirstOrDefault();
                            list.Add(new { Key = item.Type, Module = "FiremisReport", Value = fireMisreport, Remark = "误报申请|误报审批" });
                            break;
                        #endregion
                        #region  火灾报灭
                        case (int)RecordType.火灾报灭:
                            var fireReportOut =
                                       (from a in ef.Firereportout.Where(p => p.FireId == fireId && p.ReportOutId == id).ToList()
                                        join b in ef.Userinfo on a.UserId equals b.UserId
                                        into c
                                        let d = c.FirstOrDefault()
                                        select new
                                        {
                                            a.ReportOutId,
                                            a.FireId,
                                            a.TransDatetime,
                                            a.ReportOutContent,
                                            a.Ip,
                                            a.UserId,
                                            d.RealName,
                                        }).FirstOrDefault();
                            list.Add(new { Key = item.Type, Module = "FireReportOut", Value = fireReportOut, Remark = "火灾报灭" });
                            break;
                        #endregion
                        #region 通知周边
                        case (int)RecordType.通知周边:
                            var fireInforms =
                                       (from a in ef.Fireinform.Where(p => p.FireId == fireId && (p.InformId == id || ids.Contains(p.InformId))).ToList()
                                        join b in ef.Userinfo on a.UserId equals b.UserId
                                        into c
                                        let d = c.FirstOrDefault()
                                        join e in ef.Userinfo on a.TargetUserId equals e.UserId
                                        into f
                                        let g = f.FirstOrDefault()
                                        select new
                                        {
                                            a.InformId,
                                            a.FireId,
                                            a.TransDatetime,
                                            a.InformContent,
                                            a.Ip,
                                            a.UserId,
                                            d.RealName,
                                            a.TargetUserId,
                                            TargetRealName = g.RealName,
                                        }).ToList();
                            list.Add(new { Key = item.Type, Module = "FireInform", Value = fireInforms, Remark = "通知周边" });
                            break;
                        #endregion
                        #region 复燃核实|驻守调动
                        case (int)RecordType.复燃核实:
                        case (int)RecordType.驻守调动:
                            var fireStation =
                                       (from a in ef.Firestation.Where(p => p.FireId == fireId && p.StationId == id).ToList()
                                        join b in ef.Userinfo on a.UserId equals b.UserId
                                        into c
                                        let d = c.FirstOrDefault()
                                        join e in ef.Userinfo on a.SceneUserId equals e.UserId
                                        into f
                                        let g = f.FirstOrDefault()
                                        join h in ef.Firestationattach on a.StationId equals h.StationId
                                        into i
                                        let j = i.ToList()
                                        select new
                                        {
                                            a.StationId,
                                            a.FireId,
                                            a.TransDatetime,
                                            a.StationContent,
                                            a.IsReburn,
                                            a.RecordType,
                                            a.SceneTime,
                                            a.SceneUserId,
                                            SceneUserName = g == null ? string.Empty : g.RealName,
                                            a.SendMode,
                                            a.SendStatus,
                                            a.Ip,
                                            a.UserId,
                                            d.RealName,
                                            EstimateLatitude = a.EstimateLatitude,
                                            EstimateLongitude = a.EstimateLongitude,
                                            EstimateAltitude = a.EstimateAltitude,
                                            ReviseLatitude = a.ReviseLatitude,
                                            ReviseLongitude = a.ReviseLongitude,
                                            ReviseAltitude = a.ReviseAltitude,
                                            Attachs=j,
                                        }).FirstOrDefault();
                            list.Add(new { Key = item.Type, Module = "FireStation", Value = fireStation, Remark = "驻守调动|复燃核实" });
                            break;
                        #endregion
                        #region 成立前指
                        case (int)RecordType.成立前指:
                            var fireFrontCommand =
                                       (from a in ef.Firefrontcommand.Where(p => p.FireId == fireId && p.FrontCommandId == id).ToList()
                                        join b in ef.Userinfo on a.UserId equals b.UserId
                                        into c
                                        let d = c.FirstOrDefault()
                                        select new FireFrontCommandModel
                                        {
                                            FrontCommandId = a.FrontCommandId,
                                            FireId = a.FireId,
                                            TransDatetime = a.TransDatetime,
                                            CommanderName = a.CommanderName,
                                            CommanderTel = a.CommanderTel,
                                            HeadquarterMemberIds = string.IsNullOrEmpty(a.HeadquarterMemberIds)
                                                               ? new int[] { }
                                                               : a.HeadquarterMemberIds.Split(',').Select(s => Convert.ToInt32(s)).ToArray(),
                                            Latitude = a.Latitude,
                                            Longitude = a.Longitude,
                                            Ip = a.Ip,
                                            UserId = a.UserId,
                                            RealName = d.RealName,
                                        }).FirstOrDefault();
                            if (fireFrontCommand != null && fireFrontCommand.HeadquarterMemberIds != null && fireFrontCommand.HeadquarterMemberIds.Length > 0)
                            {
                                var fhmLinq = from a in ef.Fireheadquartermember.Where(p => fireFrontCommand.HeadquarterMemberIds.Contains(p.HeadquarterMemberId)).ToList()
                                              join b in ef.Fireheadquarter on a.HeadquarterId equals b.HeadquarterId
                                              into c
                                              let d = c.FirstOrDefault()
                                              join e in ef.Position on a.PositionId equals e.PositionId
                                              into f
                                              let g = f.FirstOrDefault()
                                              join h in ef.Fireteamdepartment on a.TeamDepartmentId equals h.TeamDepartmentId
                                              into i
                                              let j = i.FirstOrDefault()
                                              select new
                                              {
                                                  a.HeadquarterId,
                                                  a.HeadquarterMemberId,
                                                  a.Tel,
                                                  a.TeamDepartmentId,
                                                  a.TransDatetime,
                                                  a.PositionId,
                                                  a.Phone,
                                                  HeadquarterName = d == null ? string.Empty : d.Name,
                                                  HeadquarterMemberName = a.Name,
                                                  PositionName = g == null ? string.Empty : g.PositionName,
                                                  TeamDepartmentName = j == null ? string.Empty : j.Name
                                              };
                                fireFrontCommand.HeadquarterMembers = fhmLinq.ToList();
                            }

                            list.Add(new { Key = item.Type, Module = "FireFrontCommand", Value = fireFrontCommand, Remark = "成立前指" });
                            break;
                        #endregion
                        #region 启动响应
                        case (int)RecordType.启动响应:
                            var fireResponse =
                                       (from a in ef.Fireresponse.Where(p => p.FireId == fireId && p.ResponseId == id).ToList()
                                        join b in ef.Userinfo on a.UserId equals b.UserId
                                        into c
                                        let d = c.FirstOrDefault()
                                        select new
                                        {
                                            a.ResponseId,
                                            a.FireId,
                                            a.Level,
                                            a.TransDatetime,
                                            a.ResponseContent,
                                            a.Ip,
                                            a.UserId,
                                            d.RealName,
                                        }).FirstOrDefault();
                            list.Add(new { Key = item.Type, Module = "FireResponse", Value = fireResponse, Remark = "启动响应" });
                            break;
                        #endregion
                        #region 调集扑救力量
                        case (int)RecordType.扑救力量:
                            var fireFightingForce =
                                       (from a in ef.Firefightingforce.Where(p => p.FireId == fireId && (p.FightingForceId == id || ids.Contains(p.FightingForceId))).ToList()
                                        join b in ef.Userinfo on a.UserId equals b.UserId
                                        into c
                                        let d = c.FirstOrDefault()
                                        join e in ef.Fireheadquarter on a.HeadquarterId equals e.HeadquarterId
                                        into f
                                        let g = f.FirstOrDefault()
                                        select new
                                        {
                                            a.FightingForceId,
                                            a.FireId,
                                            a.Status,
                                            a.TeamNum,
                                            a.TransDatetime,
                                            a.HeadquarterId,
                                            a.Ip,
                                            a.UserId,
                                            d.RealName,
                                            HeadquarterName = g.Name,
                                        }).ToList();
                            list.Add(new { Key = item.Type, Module = "FireFightingForce", Value = fireFightingForce, Remark = "调集扑救力量" });
                            break;
                        #endregion
                        #region 联动响应
                        case (int)RecordType.联动响应:
                            var fireTeamResponse =
                                       (from a in ef.Fireteamresponse.Where(p => p.FireId == fireId && (p.TeamResponseId == id || ids.Contains(p.TeamResponseId))).ToList()
                                        join b in ef.Userinfo on a.UserId equals b.UserId
                                        into c
                                        let d = c.FirstOrDefault()
                                        join e in ef.Fireteamdepartment on a.TeamDepartmentId equals e.TeamDepartmentId
                                        into f
                                        let g = f.FirstOrDefault()
                                        select new
                                        {
                                            a.TeamResponseId,
                                            a.TeamDepartmentId,
                                            TeamDepartmentName = g == null ? string.Empty : g.Name,
                                            a.FireId,
                                            a.Status,
                                            a.TeamResponseContent,
                                            a.TransDatetime,
                                            a.Ip,
                                            a.UserId,
                                            d.RealName,
                                        }).ToList();
                            list.Add(new { Key = item.Type, Module = "FireTeamResponse", Value = fireTeamResponse, Remark = "联动响应" });
                            break;
                        #endregion
                        #region 火灾终报
                        case (int)RecordType.火灾终报:
                            var firefinalreport =
                                       (from a in ef.Firefinalreport.Where(p => p.FireId == fireId && p.FinalReportId == id).ToList()
                                        join b in ef.Userinfo on a.UserId equals b.UserId
                                        into c
                                        let d = c.FirstOrDefault()
                                        join e in ef.Firealarmtype on a.AlarmTypeId equals e.TypeId
                                        into f
                                        let g = f.FirstOrDefault()
                                        join h in ef.Firecause on a.CauseId equals h.CauseId
                                        into i
                                        let j = i.FirstOrDefault()
                                        join u in ef.Fireinfo on a.FireId equals u.FireId
                                        into v
                                        let w = v.FirstOrDefault()
                                        select new
                                        {
                                            a.FinalReportId,
                                            a.FireId,
                                            a.AlarmTypeId,
                                            a.UserId,
                                            a.CauseId,
                                            a.Ip,
                                            a.TownPeopleSum,
                                            a.BurnedArea,
                                            a.BurnedAreaUnit,
                                            a.BurnedTreeAge,
                                            a.BurnedTreeDieSum,
                                            a.ArmyPeopleSum,
                                            a.ArmyRankSum,
                                            a.BurnedTreeSum,
                                            a.BurnedTreeTypeSum,
                                            a.Casualties,
                                            a.FinalReportContent,
                                            a.ForestBurnedArea,
                                            a.ForestBurnedAreaUnit,
                                            a.Humidity,
                                            a.IsSupplementary,
                                            a.Leader,
                                            a.OtherSituation,
                                            a.ProfessionalPeopleSum,
                                            a.ProfessionalRankSum,
                                            a.PutOutTime,
                                            a.ReportTime,
                                            a.StationPeopleSum,
                                            a.StationRankSum,
                                            a.Temperature,
                                            a.TownRankSum,
                                            a.TransDatetime,
                                            a.Weather,
                                            a.WindDirection,
                                            a.WindPower,
                                            d.RealName,
                                            AlarmTypeName = g == null ? string.Empty : g.TypeName,
                                            CauseName = j == null ? string.Empty : j.CauseName,
                                            w.AlarmTime,
                                            w.Address,
                                            a.CaseFiles,
                                            a.IdentifyFiles
                                        }).FirstOrDefault();
                            list.Add(new { Key = item.Type, Module = "FireFinalReport", Value = firefinalreport, Remark = "火灾终报" });
                            break;
                        #endregion
                        #region 定位火点
                        case (int)RecordType.定位火点:
                            var firesite = ef.Firesite.Where(p => p.FireId == fireId && p.SiteId == id).FirstOrDefault();
                            list.Add(new { Key = item.Type, Module = "fireSite", Value = firesite, Remark = "定位火点" });
                            break;
                        #endregion
                    }
                }
                result.FirstParam = list;
                return result;
            }
        }

        public Fireinfo Get(int fireId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                return ef.Fireinfo.Where(p => p.FireId == fireId).FirstOrDefault();
            }
        }
    }
}
